package cn.wolfcode.luowowo.webSite.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/destination")
public class DestinationController {

    @Reference
    private IRegionService regionService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyDetailService detailService;

    @Reference
    private ITravelService travelService;

    @RequestMapping("")
    public String index(Model model){
        model.addAttribute("hotRegions",regionService.queryHotRegions());
        return "/destination/index";
    }


    @RequestMapping("/getHotDestByRegionId")
    public String getHotDestByRegionId(Model model,Long regionId){

        //查询数据
        List<Destination> list = destinationService.getDestByRegionId(regionId);

        //拆分数据
        int size = list.size();

        //左边:leftDests
        model.addAttribute("leftDests", list.subList(0, size / 2 ));

        //右边:rightDests
        model.addAttribute("rightDests", list.subList(size / 2 , size));

        //拼接HTML模板
        model.addAttribute("regionId",regionId);
        return "/destination/hotdestTpl";
    }

    @RequestMapping("/guide")
    public String guide(Model model,Long id ){

        //调用之前处理吐司的方法,其实就是一层层查询出父地区
        List<Destination> toasts = destinationService.getToasts(id);

        //把最后一层给剔除掉
        Destination dest = toasts.remove(toasts.size() - 1);

        model.addAttribute("toasts", toasts);

        //把最后一层也返回前端页面处理
        model.addAttribute("dest", dest);

        //查询攻略分类,同时关联查询攻略分类下明细
        List<StrategyCatalog> catalogs = strategyCatalogService.queryCatalogByDestId(id);
        model.addAttribute("catalogs",catalogs);

        //展示前三篇   strategyDetails
        List<StrategyDetail> strategyDetails = detailService.getDetailTop3(id);
        model.addAttribute("strategyDetails", strategyDetails);

        return "/destination/guide";
    }

    @RequestMapping("/surveyPage")
    public String surveyPage(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo){
        //调用之前处理吐司的方法,其实就是一层层查询出父地区
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());

        //把最后一层给剔除掉
        Destination dest = toasts.remove(toasts.size() - 1);

        model.addAttribute("toasts", toasts);

        //把最后一层也返回前端页面处理
        model.addAttribute("dest", dest);

        return "/destination/survey";
    }

    @RequestMapping("/survey")
    public String survey(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo){
        //当前目的地下的分类集合
        List<StrategyCatalog> catalogs = strategyCatalogService.queryCatalogByDestId(qo.getDestId());
        model.addAttribute("catalogs", catalogs);

        //选中的当前分类展示
        StrategyCatalog catalog = strategyCatalogService.get(qo.getCatalogId());
        model.addAttribute("catalog", catalog);

        //展示选中攻略分类下的第一篇攻略明细
        List<StrategyDetail> details = catalog.getDetails();
        if(details!=null&&details.size()>0){
            StrategyDetail detail = details.get(0);
            //设置明细内容
            detail.setStrategyContent(detailService.getContent(detail.getId()));
            model.addAttribute("detail", detail);
        }
        return "/destination/surveyTpl";
    }

    @RequestMapping("/travels")
    public String travels(Model model, @ModelAttribute("qo") TravelQuery qo){
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "/destination/travelTpl";
    }
}
